package com.egg.common.core.model.response;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 角色信息
 *
 * @author east
 * @date 2019/4/2 14:52
 */
@NoArgsConstructor
@Data
@ApiModel(value = "分页包装类")
public class PageData<T> implements Serializable {

    @ApiModelProperty(value = "总条数")
    private int total;

    @ApiModelProperty(value = "总页数")
    private int pages;

    @ApiModelProperty(value = "页数")
    private int current;

    @ApiModelProperty(value = "页长")
    private int pageSize;

    @ApiModelProperty(value = "数据")
    private List<T> list;

    public PageData(Page page) {
        if (ObjectUtil.isNotEmpty(page.getRecords())) {
            this.list = page.getRecords();
        } else {
            this.list = Collections.emptyList();
        }
        this.current = (int) page.getCurrent();
        this.pageSize = (int) page.getSize();
        this.total = (int) page.getTotal();
        this.pages = (int) page.getPages();
    }

    public PageData(Page page, List list) {
        if (ObjectUtil.isNotEmpty(list)) {
            this.list = list;
        } else {
            this.list = Collections.emptyList();
        }
        this.current = (int) page.getCurrent();
        this.pageSize = (int) page.getSize();
        this.total = (int) page.getTotal();
        this.pages = (int) page.getPages();
    }
}
